package com.hnkywl.sts.action.report;

import com.hnkywl.sts.service.organ.GsjgService;
import com.hnkywl.sts.service.report.BzjlxfpService;
import com.hnkywl.sts.util.POIUtil;
import net.ruixin.web.struts2.SimpleActionSupport;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Created by ccr on 2014/5/19.
 */
@Namespace("/report/bzjlxfp")
public class BzjlxfpAction extends SimpleActionSupport{
    private Long gsId;
    private String khMc;
    private Long khId;
    private String htbm;
    private int page;
    private int rows;
    private BzjlxfpService bzjlxfpService;
    private GsjgService gsjgService;

    @Action("bzjlxfp_detail")
    public String bzjlxfpDetail(){
        if(khId != null) {
            khMc = gsjgService.findGsjgDetail(khId).getJc();
        }
        return SUCCESS;
    }

    @Action("bzjlxfpList")
    public void bzjlxfpList(){
        renderJson(bzjlxfpService.bzjlxfpList(gsId,khId,htbm,page,rows));
    }

    @Action("export")
    public String export() {
        Map<String, Object> map = bzjlxfpService.bzjlxfpListExport(gsId, khId, htbm, 1, -1);
        List<Map> result = (List<Map>) map.get("rows");
        List<Map> footer = (List<Map>) map.get("footer");
        HSSFWorkbook wb = new HSSFWorkbook();
        HSSFSheet sheet = wb.createSheet("sheet1");
        POIUtil poiUtil = new POIUtil(wb);
        sheet.setDefaultRowHeightInPoints(21);
        sheet.setDefaultColumnWidth((short) 20);
        HSSFCellStyle headerStyle = poiUtil.createHeader3HSSFCellStyle(wb);
        poiUtil.fillRow(sheet, headerStyle, 0, "园区", "客户", "合同", "保证经", "总利息", "使用利息", "利息剩余");
        List<Integer> scale = Arrays.asList(0, 0, 0, 4, 4, 4, 4);
        for (int i = 1; i < result.size() + 1; i++) {
            Map item = result.get(i - 1);
            poiUtil.fillRowComplex(wb, sheet, null, i, scale, item.get("gs"), item.get("kh"), item.get("htbm"), item.get("bzj"),
                    item.get("zlx"), item.get("ysy"), item.get("sy"));
        }
        HSSFCellStyle sumStyle = wb.createCellStyle();
        HSSFFont font = wb.createFont();
        font.setColor(HSSFFont.COLOR_RED);
        sumStyle.setFont(font);
        poiUtil.fillRowComplex(wb, sheet, sumStyle, result.size() + 1, scale, "合计：", "", "",
                footer.get(0).get("bzj"), footer.get(0).get("zlx"), footer.get(0).get("ysy"), footer.get(0).get("sy"));
        try {
            response.setContentType("application/x-download");
            String filedisplay = new String(("保证金利息分配表.xls").getBytes("gb2312"), "iso8859-1");
            response.addHeader("Content-Disposition", "attachment;filename=" + filedisplay);
            wb.write(response.getOutputStream());
            response.getOutputStream().close();
        } catch (Exception e) {
            e.printStackTrace();
            renderText("位置异常，导出失败" + e.getMessage());
        }
        return null;
    }

    public Long getGsId() {
        return gsId;
    }

    public void setGsId(Long gsId) {
        this.gsId = gsId;
    }

    public Long getKhId() {
        return khId;
    }

    public void setKhId(Long khId) {
        this.khId = khId;
    }

    public String getHtbm() {
        return htbm;
    }

    public void setHtbm(String htbm) {
        this.htbm = htbm;
    }

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }

    public int getRows() {
        return rows;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    public String getKhMc() {
        return khMc;
    }

    public void setKhMc(String khMc) {
        this.khMc = khMc;
    }

    @Autowired
    public void setBzjlxfpService(BzjlxfpService bzjlxfpService) {
        this.bzjlxfpService = bzjlxfpService;
    }
    @Autowired
    public void setGsjgService(GsjgService gsjgService) {
        this.gsjgService = gsjgService;
    }
}
